package com.erp.programmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.domain.*;
import com.erp.programmer.dao.IncomingOrderMapper;
import com.erp.programmer.dto.IncomingOrderDto;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.PurchaseOrderDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.enums.InspectStateEnum;
import com.erp.programmer.enums.StorageStateEnum;
import com.erp.programmer.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈鑫
 * @since 2021-05-27
 */
@Service
@Transactional
public class IncomingOrderServiceImpl extends ServiceImpl<IncomingOrderMapper, IncomingOrder> implements IIncomingOrderService {
    @Autowired
    IPurchaseOrderService iPurchaseOrderService;
    @Autowired
    IncomingInspectionOrderService incomingInspectionOrderService;
    @Autowired
    IncomingOrderMapper incomingOrderMapper;
    @Autowired
    ISellCompanyService iSellCompanyService;
    @Autowired
    IPurchaseOrderDetailService iPurchaseOrderDetailService;
    @Autowired
    StockOrderService stockOrderService;
    /**
     * 查询入库信息
     * @param pageDto
     * @return
     */
    @Override
    public ResponseDto<PageDto<IncomingOrderDto>> getIncomingOrder(PageDto<IncomingOrderDto> pageDto) {
        QueryWrapper<IncomingOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("incoming_date");
        if ("id".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())) {
            queryWrapper.eq("id",pageDto.getSearchContent());
        }
        if (pageDto.getPage() ==null) {
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<IncomingOrder> incomingOrders = list(queryWrapper);
        List<IncomingOrderDto> incomingOrderDtoList = CopyUtil.copyList(incomingOrders, IncomingOrderDto.class);
        incomingOrderDtoList.forEach(e->{
            PurchaseOrder purchaseOrder = iPurchaseOrderService.getById(e.getPurchaseId());
            PurchaseOrderDto purchaseOrderDto = CopyUtil.copy(purchaseOrder, PurchaseOrderDto.class);
            e.setPurchaseOrderDto(purchaseOrderDto);
        });
        PageInfo<IncomingOrder> pageInfo = new PageInfo<>(incomingOrders);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(incomingOrderDtoList);
        return ResponseDto.success(pageDto);
    }

    /**
     * 添加入库单
     * @param purchaseId
     * @return
     */
    @Override
    public ResponseDto<PageDto<IncomingOrderDto>> insertIncomingOrder(String purchaseId) {
        QueryWrapper<IncomingOrder> incomingOrderQueryWrapper = new QueryWrapper<>();
        incomingOrderQueryWrapper.eq("purchase_id",purchaseId);
        if(getOne(incomingOrderQueryWrapper) != null) {
            return ResponseDto.errorByMsg(CodeMsg.INCOMING_ORDER_HAS_EXIST);
        }
        QueryWrapper<IncomingInspectionOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_id",purchaseId);
        IncomingInspectionOrder incomingInspectionOrder = incomingInspectionOrderService.getOne(queryWrapper);
        if (incomingInspectionOrder ==null || incomingInspectionOrder.getInspectState() == InspectStateEnum.NO_INSPECTION.getCode()) {
            return ResponseDto.errorByMsg(CodeMsg.ORDER_INSPECT_STATE_ERROR);
        }
        PurchaseOrder purchaseOrder = iPurchaseOrderService.getById(purchaseId);
        SellCompany sellCompany = iSellCompanyService.getById(purchaseOrder.getSellerId());
        if (sellCompany == null) {
            return ResponseDto.errorByMsg(CodeMsg.SELLER_COMPANY_NOT_EXIST);
        }
        IncomingOrder incomingOrder = new IncomingOrder();
        incomingOrder.setPurchaseId(purchaseId);
        incomingOrder.setSellerName(sellCompany.getName());
        if (incomingOrderMapper.insert(incomingOrder)<1) {
            return ResponseDto.errorByMsg(CodeMsg.INCOMING_ORDER_ADD_ERROR);
        }
        if (!iPurchaseOrderService.updateStorageState(purchaseId,StorageStateEnum.STORED.getCode())) {
            return ResponseDto.errorByMsg(CodeMsg.PURCHASE_STORAGE_STATE_EDIT_ERROR);
        }
        // 修改库存单商品数量
        QueryWrapper<PurchaseOrderDetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("purchase_id",purchaseId);
        List<PurchaseOrderDetail> purchaseOrderDetailList = iPurchaseOrderDetailService.list(queryWrapper1);
        purchaseOrderDetailList.forEach(e->{
            QueryWrapper<StockOrder> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("product_id",e.getProductId());
            StockOrder stockOrder = stockOrderService.getOne(queryWrapper2);
            BigDecimal stockNum = new BigDecimal(stockOrder.getNumber());
            BigDecimal productNum = new BigDecimal(e.getNumber());
            BigDecimal totalNum = stockNum.add(productNum);
            stockOrder.setNumber(totalNum.toString());
            stockOrderService.updateById(stockOrder);
        });
        ResponseDto<PageDto<IncomingOrderDto>> incomingOrder1 = getIncomingOrder(new PageDto<IncomingOrderDto>());
        incomingOrder1.setMsg("添加成功");
        return incomingOrder1;
    }
}
